# -*- encoding=utf8 -*-

__author__ = "code哥"
import os
from sanic.blueprints import Blueprint
from sanic.views import HTTPMethodView
from sanic import response
from sanic_openapi import doc

blueprint = Blueprint(__name__, '/api')


class DevicesList(HTTPMethodView):

    @doc.summary("当前连接的设备")
    def post(self, request):
        devices = DevicesList.android_devices()
        if len(devices) <= 0:
            return response.json({'code': 0, 'data': ['设备读取失败，请查看是否连接成功']})
        return response.json({'code': 0, 'data': devices})

    @staticmethod
    def android_devices() -> []:
        devices_list = []
        path = '{path}/conf/devices_list.conf'.format(path=os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
        with open(path) as f:
            for line in f.readlines():
                if len(line.strip()) > 0 and len(line.replace("\r", "").replace("\n", "").replace("\r\n", "")) > 0:
                    devices_list.append(line.replace("\r", "").replace("\n", "").replace("\r\n", ""))
        return devices_list


blueprint.add_route(DevicesList.as_view(), '/devices_list', strict_slashes=True)
